package com.mysafe.provision.model.expense;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.inject.Inject;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.mysafe.provision.model.account.Account;
import com.mysafe.provision.model.cycle.Cycle;
import com.mysafe.provision.model.tag.Tag;

@Entity
@Table(schema = "sfe", name = "en_expense")
@SequenceGenerator(schema = "sfe", name = "seq_en_expense", allocationSize = 1)
public class Expense implements Serializable {

	private static final long serialVersionUID = -3179160302348048959L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_en_expense")
	@Column(name = "id_expense")
	private Integer id;
	
	private Double value;
	
	@Temporal(TemporalType.DATE)
	private Date date;
	
	@ManyToMany(targetEntity = Tag.class, cascade = CascadeType.PERSIST)
	@JoinTable(schema = "sfe", name = "re_expense_tags", 
			joinColumns = @JoinColumn(name = "id_expense"), 
			inverseJoinColumns = @JoinColumn(name = "id_tag"))
	private Set<Tag> tags;
	
	@OneToOne(cascade = CascadeType.ALL)
	@JoinColumn(name = "id_cycle") 
	private Cycle cycle;
	
	@OneToOne
	@JoinColumn(name = "id_account") 
	private Account account;

	public Expense() {
	}
	
	@Inject
	public Expense(Cycle cycle, Account account) {
		this.cycle = cycle;
		this.account = account;
		this.tags = new HashSet<Tag>();
	}
	
	public Double getValue() {
		return this.value;
	}

	public void setValue(Double value) {
		this.value = value;
	}

	public Cycle getCycle() {
		return this.cycle;
	}

	public void setCycle(Cycle cycle) {
		this.cycle = cycle;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return id;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public Account getAccount() {
		return account;
	}

	public void setTags(Set<Tag> tags) {
		this.tags = tags;
	}

	public Set<Tag> getTags() {
		return tags;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Date getDate() {
		return date;
	}
}